package org.come.login;

import java.awt.*;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import org.come.until.Music;
import org.come.until.Util;
import org.come.view.View;

public class KeyView extends View {
	//创建键盘按钮组
	private JButton[] keyButton;
	public KeyView(LoginView loginView) {
		// 键盘监听
		this.setPreferredSize(new Dimension(700, 200));
		String[] keyBord={"a","A","b","B","c","C","d","D","e","E","f","F","g","G"
				,"h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P"
				,"q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z"
				,"0","1","2","3","4","5","6","7","8","9","`","~","!","@","#","$","%","^","&","*","(",")","_","+"
				,"-","=","<",">",",",".","/","?",";",":","'","'","|","\\"};
		keyButton=new JButton[keyBord.length];
		// 及那盘按钮初始化
		keyButtonInit(keyBord,loginView);
		// 创建键盘按钮进行
		for (int i = 0; i < keyButton.length; i++) {
			keyButton[i].setBounds(i%18*29+80, i/18*29+30, 27, 27);
			this.add(keyButton[i]);
		}
	}
	private ImageIcon icon;
	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		if (icon == null){
			icon = new ImageIcon("resource/NewUi/键盘背景.png");
		}
		g.drawImage(icon.getImage(), 0, 0, 700, 200, this);
	}
	/** 进行键盘按钮初始化 */
	public void keyButtonInit(String[] keyBord,LoginView loginView) {
		sortingInsanity(keyBord);// 键盘进行错乱
		ImageIcon icon = new ImageIcon("resource/NewUi/按键.png");
		icon.setImage(icon.getImage().getScaledInstance(27, 27,Image.SCALE_DEFAULT));
		for (int i = 0; i < keyBord.length; i++) {
			keyButton[i] = new JButton(keyBord[i], icon);
			keyButton[i].setOpaque(false);// 设置控件是否透明，true为不透明，false为透明
			keyButton[i].setContentAreaFilled(true);// 设置图片填满按钮所在的区域
			keyButton[i].setMargin(new Insets(0, 0, 0, 0));// 设置按钮边框和标签文字之间的距离
			keyButton[i].setFocusPainted(false);// 设置这个按钮是不是获得焦点
			keyButton[i].setBorderPainted(false);// 设置是否绘制边框
			keyButton[i].setBorder(null);// 设置边框
			// 设置icon的大小
			keyButton[i].setHorizontalTextPosition(SwingConstants.CENTER);
			keyButton[i].setForeground(Color.white);
			keyButton[i].setFont(new Font("Arial", 0, 16));
			keyButton[i].setText(keyBord[i]);
			keyButton[i].addMouseListener(new KeyMouslisten(keyBord[i],loginView));
		}
	}
	/** 错乱排序算法 */
	public void sortingInsanity(String[] keyBord) {
		String valuse = null;
		for (int i = 0; i < keyBord.length; i++) {
			int positionone = Util.random.nextInt(keyBord.length);// 第一个位置
			int positiononetwo = Util.random.nextInt(keyBord.length);// 第二个位置
			// 将两个随机位置的顺序进行错乱
			if (positionone != positiononetwo) {
				valuse = keyBord[positionone];
				keyBord[positionone] = keyBord[positiononetwo];
				keyBord[positiononetwo] = valuse;
			}
		}
	}
}
